package com.ywyt.elasticsearch.service.impl;

import com.ywyt.elasticsearch.service.DocService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class DocServiceImpl implements DocService {

    @Autowired
    private RestHighLevelClient client;
    @Override
    public void addDoc(String index, String type, String json) throws IOException {
        //1.添加文档的request
        IndexRequest indexRequest = new IndexRequest(index,type);

        //2.设置文档内容
        indexRequest.source(json, XContentType.JSON);

        //3.执行添加
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);

        //4.输出结果
        log.info("【添加文档】response = {}",response);
    }

    @Override
    public void addDoc(String index, String type, String json, String id) throws IOException {
        //1.添加文档的request
        IndexRequest indexRequest = new IndexRequest(index,type,id);

        //2.设置文档内容
        indexRequest.source(json, XContentType.JSON);

        //3.执行添加
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);

        //4.输出结果
        log.info("【添加文档】response = {}",response);
    }

    @Override
    public void updateDoc(String index, String type, String id, Map<String, Object> doc) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(index,type,id);
        updateRequest.doc(doc);
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
        log.info("【修改文档】update = {}",update);
    }

    @Override
    public void deleteDoc(String index, String type, String id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        log.info("【删除文档】delete = {}",delete);
    }

    @Override
    public void bulkAddOrUpdate(String index, String type, String[] ids, String [] jsons) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        for (int i = 0; i < ids.length; i++) {
            bulkRequest.add(new IndexRequest(index, type,ids[i]).source(jsons[i],XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("【批量添加或修改】bulk = {}",bulk);
    }

    @Override
    public void bulkDelete(String index, String type, String[] ids) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        for (String id : ids) {
            bulkRequest.add(new DeleteRequest(index,type,id));
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.info("【批量删除】bulk = {}",bulk);
    }
}
